<template>
  <Form class="xtx-form" ref="formCom" :validation-schema="veeValidateSchema" v-slot="{ errors }">
    <h3>{{errors}}</h3>
    <h2>unionId:{{unionId}}</h2>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field name='accountApi' v-model="form.account" :class="{error:errors.accountApi}" class="input" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error" v-if="errors.accountApi">{{errors.accountApi}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field name='mobile' v-model="form.mobile" :class="{error:errors.mobile}" class="input" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error" v-if="errors.mobile">{{errors.mobile}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field name='code' v-model="form.code" :class="{error:errors.code}" class="input" type="text" placeholder="请输入验证码" />
        <span class="code" @click="sendCodeBtn">{{number===0?'发送验证码':number+'秒后发送'}}</span>
      </div>
      <div class="error" v-if="errors.code">{{errors.code}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field name='password' v-model="form.password" :class="{error:errors.password}" class="input" type="password" placeholder="请输入密码" />
      </div>
      <div class="error" v-if="errors.password">{{errors.password}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field name='rePassword' :class="{error:errors.rePassword}" class="input" type="password" placeholder="请确认密码" />
      </div>
      <div class="error" v-if="errors.rePassword">{{errors.rePassword}}</div>
    </div>
    <a href="javascript:;" class="submit" @click="loginBtn">立即提交</a>
  </Form>
</template>

<script>
import { Field, Form } from 'vee-validate'
import veeValidateSchema from '@/utils/vee-validate-schema'
import { reactive, ref, watch } from 'vue'
import { userQQPatchCode, userQQPatchLogin } from '@/api/user'
import { login, sendCode } from '@/utils/formbtn'
export default {
  name: 'CallbackPatch',
  components: {
    Field, Form
  },
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const form = reactive({
      unionId: '',
      account: '',
      mobile: '',
      code: '',
      password: ''
    })
    watch(() => props.unionId, () => {
      form.unionId = props.unionId
      // 防止其他页面切换过来时不能获取到Id
    }, { immediate: true })

    // 定义表单的ref
    const formCom = ref(null)

    // 登录按钮点击事件
    const loginBtn = async () => {
      login(userQQPatchLogin, form, formCom)
    }

    const number = ref(0)
    const sendCodeBtn = () => {
      // 发送验证码封装的函数
      sendCode(userQQPatchCode, form.mobile, formCom, number)
    }
    return { veeValidateSchema, form, loginBtn, formCom, sendCodeBtn, number }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
